package com.xl.adminserver.v2.system;

import com.google.common.collect.Maps;
import com.xl.adminserver.beans.RestApiResult;
import com.xl.adminserver.v2.clients.ITestClient;
import com.xl.adminserver.v2.dispatch.BikeDispatchAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

@RestController
@RequestMapping("/api/test")
public class TestController {
    @Autowired
    private ITestClient testClient;
    @Autowired
    private BikeDispatchAction bikeDispatchAction;

    @RequestMapping("list")
    public List<String> getList() throws InterruptedException {
        //并发测试1
        CountDownLatch countDownLatch = new CountDownLatch(30);
        for (int i = 0; i < 30; i++) {
            new Thread() {
                @Override
                public void run() {
                    countDownLatch.countDown();
                    List<String> list = testClient.getList("qwe123");
                    System.out.println("-------" + list);
                }
            }.start();
        }
        countDownLatch.await();
        System.out.println("当前线程：" + Thread.currentThread().getName());
        //

        bikeDispatchAction.test1();
        return null;
    }

    @RequestMapping("list1")
    public List<String> getList1() throws InterruptedException {
        final CyclicBarrier cyclicBarrier = new CyclicBarrier(30, () -> {
            System.out.println("所有人都准备好了裁判开始了");

        });
        for (int i = 0; i < 30; i++) {
            //lambda中只能只用final的变量
            final int times = i;
            new Thread(() -> {
                try {
                    System.out.println("子线程" + Thread.currentThread().getName() + "准备---");
                    cyclicBarrier.await();
                    System.out.println("子线程" + Thread.currentThread().getName() + "开始跑了");
                    List<String> list = testClient.getList("qwe123");
                    System.out.println("-------" + list);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();
            System.out.println("当前线程：" + Thread.currentThread().getName());
        }
        bikeDispatchAction.test1();
        return null;
    }




    @RequestMapping("postBody")
    public RestApiResult postBody() throws IOException {
        Map<String, Object> params = Maps.newHashMap();
        params.put("test", "1111");
        return this.bikeDispatchAction.postBody(params);
    }


//    @RequestMapping("dispatchList")
//    public Object dispatchList() throws IOException {
//        Map<String, Object> params = Maps.newHashMap();
//        params.put("id", 1);
//        params.put("p", "1");
//        params.put("name", "1111");
//        return this.bikeDispatchAction.dispatch("1111", params);
//    }
//
//    @RequestMapping("dispatchList2")
//    public Object dispatchList2() throws IOException {
//        Map<String, Object> params = Maps.newHashMap();
//        params.put("id", 2);
//        params.put("p", "2");
//        params.put("name", "2222");
//        return this.bikeDispatchAction.dispatch("1111", params);
//    }


    @GetMapping(value = {"", "welcome"})
    public String welcome() {
        return "Welcome!!!";
    }

    @GetMapping("index")
    public String index() {
        return "index!!!";
    }

    @GetMapping("admin")
    public String admin() {
        return "admin!!!";
    }

    @GetMapping("user")
    public String user() {
        return "user!!!";
    }

    @GetMapping("customer")
    public String customer() {
        return "customer!!!";
    }

    /**
     * 方法执行前鉴权
     *
     * @return
     */
    @GetMapping("roleAdmin")
    @Secured("ROLE_ADMIN")
    public String roleAdmin() {
        return "roleAdmin!!!";
    }

    /**
     * 方法执行前鉴权
     *
     * @return
     */
    @GetMapping("preAuthorize")
    @PostAuthorize("hasAnyRole('ROLE_ADMIN')")
    public String preAuthorize() {
        System.out.println("preAuthorize…………");
        return "preAuthorize!!!";
    }

    /**
     * 方法执行完再鉴权
     *
     * @return
     */
    @GetMapping("postAuthorize")
    @PostAuthorize("hasAnyRole('ROLE_USER')")
    public String postAuthorize() {
        System.out.println("postAuthorize…………");
        return "PostAuthorize!!!";
    }

}
